import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter_app/models/ProjectType.dart';
import 'package:flutter_app/widgets/HYSizeFit.dart';

class ChoosePjTypeRoute extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ChoosePjTypeRouteState();//类名后要加()
  }
}

class _ChoosePjTypeRouteState extends State<ChoosePjTypeRoute>{
  List<ProjectType> ProjectTypeList = ProjectType.ProjectTypeList;
  final List<String> images = [
    'https://images.unsplash.com/photo-1586882829491-b81178aa622e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
    'https://images.unsplash.com/photo-1586871608370-4adee64d1794?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2862&q=80',
    'https://images.unsplash.com/photo-1586901533048-0e856dff2c0d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
    'https://images.unsplash.com/photo-1586902279476-3244d8d18285?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
    'https://images.unsplash.com/photo-1586943101559-4cdcf86a6f87?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1556&q=80',
    'https://images.unsplash.com/photo-1586951144438-26d4e072b891?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
    'https://images.unsplash.com/photo-1586953983027-d7508a64f4bb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
  ];
  @override
  void initState() {

  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
          centerTitle: true,
          title: Text(
              '发布项目',
              style: TextStyle(
                fontSize: 20.0,
                height: 1.2,
              )
          ),
          backgroundColor: Colors.white,
          elevation: 0, //去掉Appbar底部阴影
          brightness: Brightness.light,//状态栏颜色
          leading: Builder(builder: (context) {
            return new IconButton(
                icon: new Icon(Icons.arrow_back_ios,color: Colors.grey,),
                tooltip: '退出',
                onPressed: ()=>{Navigator.pop(context)}
            );
          })
      ),
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top:0,left:16,bottom:0),
              child: RichText(
                text: TextSpan(
                  text: '请选择您的加工订单类型：',
                  style: TextStyle(
                      color: Colors.black54,
                      fontSize: 15.0,
                    fontWeight:FontWeight.w500,
                  ),
                ),
              ),
            ),
            Container(
                child: CarouselSlider.builder(
                  itemCount: ProjectTypeList.length,
                  options: CarouselOptions(
                    autoPlay: false,
                    aspectRatio: 0.7,
                    enlargeCenterPage: true,
                  ),
                  itemBuilder: (context, index, realIdx) {
                    return Container(
                      decoration: new BoxDecoration(
                        border: new Border.all(
                          color: Colors.white,
                          width: 0
                        ),
                        borderRadius: new BorderRadius.all(Radius.circular(20)),
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black26,
                              blurRadius: 10.0,
                              spreadRadius: 0.5
                          )
                        ]
                      ),
                      margin: EdgeInsets.all(HYSizeFit.setPx(10)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Image.asset(ProjectTypeList[index].imagePath),
                          Padding(
                            padding: EdgeInsets.all(10),
                          ),
                          RichText(
                            text: TextSpan(
                              text: ProjectTypeList[index].typeName,
                              style: TextStyle(
                                color: Colors.black87,
                                fontSize: 25.0,
                                fontWeight:FontWeight.bold,
                                fontFamily: 'Roboto',
                                letterSpacing: 0.6
                              ),
                            ),
                          ),
                          RichText(
                            text: TextSpan(
                              text: ProjectTypeList[index].typeInfo.join('\n'),
                              style: TextStyle(
                                  color: Colors.black54,
                                  fontSize: 15.0,
                                  fontWeight:FontWeight.normal,
                                  fontFamily: 'Roboto',
                                  letterSpacing: 0.6
                              ),
                            ),
                            textAlign: TextAlign.center,
                          ),
                          TextButton(
                            onPressed: (){

                            },
                            style: ButtonStyle(
                              backgroundColor: MaterialStateProperty.all(Colors.blueGrey),
                                foregroundColor: MaterialStateProperty.all(Colors.white),
                              shape: MaterialStateProperty.all(RoundedRectangleBorder(
                                side: BorderSide(
                                  width: 1,
                                  //color: Colors.black54
                                ),
                                borderRadius: BorderRadius.circular(10),
                              ),
                              ),
                              padding: MaterialStateProperty.all(EdgeInsets.all(10)),
                              elevation: MaterialStateProperty.all(5),
                            ),
                            child: Text(
                              ProjectTypeList[index].buttonTxt,
                              style: TextStyle(
                                  //color: Colors.black54,
                                  fontSize: 15.0,
                                  fontWeight:FontWeight.normal,
                                  fontFamily: 'Roboto',
                                  letterSpacing: 0.6
                              ),
                            ))
                        ],
                      )
                    );
                  },
                )
            ),
          ],
        ),
      ),
      resizeToAvoidBottomPadding: false,
    );
  }
}

class PrefetchImageDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PrefetchImageDemoState();
  }
}

class _PrefetchImageDemoState extends State<PrefetchImageDemo> {
  final List<String> images = [
    'https://images.unsplash.com/photo-1586882829491-b81178aa622e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
    'https://images.unsplash.com/photo-1586871608370-4adee64d1794?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2862&q=80',
    'https://images.unsplash.com/photo-1586901533048-0e856dff2c0d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
    'https://images.unsplash.com/photo-1586902279476-3244d8d18285?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
    'https://images.unsplash.com/photo-1586943101559-4cdcf86a6f87?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1556&q=80',
    'https://images.unsplash.com/photo-1586951144438-26d4e072b891?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
    'https://images.unsplash.com/photo-1586953983027-d7508a64f4bb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
  ];

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      images.forEach((imageUrl) {
        precacheImage(NetworkImage(imageUrl), context);
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Prefetch image slider demo')),
      body: Container(
          child: CarouselSlider.builder(
            itemCount: images.length,
            options: CarouselOptions(
              autoPlay: true,
              aspectRatio: 2,
              enlargeCenterPage: true,
            ),
            itemBuilder: (context, index, realIdx) {
              return Container(
                child: Center(
                    child: Image.network(images[index], fit: BoxFit.cover, width: 1000)
                ),
              );
            },
          )
      ),
    );
  }
}

